# -*- coding: utf-8 -*-
import scrapy
from myproject.items import JrSearchItem
import codecs
import json
from scrapy.http import Request
from scrapy.http import FormRequest
from scrapy.utils.response import open_in_browser

class JdSpider(scrapy.Spider):
    name = 'jd'
    allowed_domains = ['jrsearch.jd.com']
    start_urls = ['https://jrsearch.jd.com/gs/search?q=%E4%BC%97%E7%AD%B9/']

    def parse(self, response):
        self.start_requests(self)
        pass

    def start_requests(self):
        url = "https://jrsearch.jd.com/gs/getList?pagesize=10"
        requests = []
        for i in range(1, 72):
            formdata = {"q": "众筹",
                        "fenlei": "A",
                        "page": str(i)}
            request = FormRequest(url, callback=self.parse_model, formdata=formdata)
            requests.append(request)
        return requests

    def parse_model(self, response):
        jsonBody = json.loads(response.body.decode('utf-8'))
        models = jsonBody['list']
        modelItems = []
        for dict in models:
            modelItem = JrSearchItem()
            modelItem['item_title'] = dict['projectName']
            modelItem['is_sucess'] = dict['projectStatus']
            modelItem['item_type'] = dict['type']
            modelItem['item_url'] = dict['projectId']

            modelItem['amount'] = dict['amount']
            modelItem['collectedAmount'] = dict['collectedAmount']
            modelItem['projectId'] = dict['projectId']
            modelItem['topLimit'] = dict['topLimit']
            modelItems.append(modelItem)
        return modelItems



